home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtwidget / controls.c.z / controls.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  14.6 KB  |  494 lines

  1. /*
  2.  * controls.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: controls.c /main/cde1_maint/1 1995/07/17 16:45:32 drk $ */
  26. /*
  27.  * (c) Copyright 1993, 1994 Hewlett-Packard Company    
  28.  * (c) Copyright 1993, 1994 International Business Machines Corp.
  29.  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  30.  * (c) Copyright 1993, 1994 Novell, Inc.
  31.  */
  32.  
  33.  
  34. /*
  35.  * controls.c
  36.  *
  37.  * Example code for libDtWidget controls
  38.  * (ComboBox, SpinBox, MenuButton)
  39.  */
  40.  
  41. #include <Xm/XmAll.h>
  42.  
  43. #include <Dt/ComboBox.h>
  44. #include <Dt/SpinBox.h>
  45. #include <Dt/MenuButton.h>
  46.  
  47. #define ApplicationClass "Controls"
  48.  
  49. static void CreateSpinBoxes(Widget);
  50. static void CreateComboBoxes(Widget);
  51. static void CreateMenuButtons(Widget);
  52.  
  53. main(int argc, char **argv)
  54. {
  55.     XtAppContext appContext;
  56.     Arg args[20];
  57.     int n;
  58.  
  59.     Widget toplevel, mainWindow, spinContainer, comboContainer, menuContainer;
  60.     
  61.     toplevel = XtAppInitialize(&appContext, ApplicationClass, NULL, 0,
  62.                         &argc, argv, NULL, NULL, 0);
  63.  
  64.     n = 0;
  65.     XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
  66.     XtSetArg(args[n], XmNspacing, 40); n++;
  67.     mainWindow = XmCreateWorkArea(toplevel, "mainWindow", args, n);
  68.     XtManageChild(mainWindow);
  69.  
  70.     n = 0;
  71.     XtSetArg(args[n], XmNspacing, 20); n++;
  72.     spinContainer = XmCreateWorkArea(mainWindow, "spinContainer", args, n);
  73.     XtManageChild(spinContainer);
  74.     CreateSpinBoxes(spinContainer);
  75.  
  76.     n = 0;
  77.     XtSetArg(args[n], XmNspacing, 20); n++;
  78.     comboContainer = XmCreateWorkArea(mainWindow, "comboContainer", args, n);
  79.     XtManageChild(comboContainer);
  80.     CreateComboBoxes(comboContainer);
  81.  
  82.     n = 0;
  83.     XtSetArg(args[n], XmNspacing, 20); n++;
  84.     menuContainer = XmCreateWorkArea(mainWindow, "menuContainer", args, n);
  85.     XtManageChild(menuContainer);
  86.     CreateMenuButtons(menuContainer);
  87.  
  88.     XtRealizeWidget(toplevel);
  89.     XtAppMainLoop(appContext);
  90. }
  91.  
  92.  
  93. /*
  94.  * Example code for DtSpinBox
  95.  */
  96.  
  97. static char *spinValueStrings[] = {
  98.     "alpha", "beta", "gamma", "delta",
  99.     "epsilon", "zeta", "eta", "theta",
  100.     "iota", "kappa", "lambda", "mu",
  101.     "nu", "xi", "omicron", "pi",
  102.     "rho", "sigma", "tau", "upsilon",
  103.     "phi", "chi", "psi", "omega"
  104. };
  105.  
  106. static void ModifyVerifyCb(Widget, XtPointer, XtPointer);
  107.  
  108. static void CreateSpinBoxes(Widget parent)
  109. {
  110.     Widget titleLabel, spinBox;
  111.     XmString *valueXmstrings;
  112.     int numValueStrings;
  113.     XmString labelString;
  114.     Arg args[20];
  115.     int i, n;
  116.     
  117.     /* Create value compound strings */
  118.  
  119.     numValueStrings = XtNumber(spinValueStrings);
  120.     valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
  121.     for (i = 0; i < numValueStrings; i++) {
  122.     valueXmstrings[i] = XmStringCreateLocalized(spinValueStrings[i]);
  123.     }
  124.  
  125.     /* Create title label */
  126.  
  127.     labelString = XmStringCreateLocalized("SpinBox Widget");
  128.     n = 0;
  129.     XtSetArg(args[n], XmNlabelString, labelString); n++;
  130.     titleLabel = XmCreateLabel(parent, "title", args, n);
  131.     XtManageChild(titleLabel);
  132.     XmStringFree(labelString);
  133.  
  134.  
  135.     /*
  136.      * Create a SpinBox containing string values.
  137.      */
  138.  
  139.     n = 0;
  140.     XtSetArg(args[n], DtNvalues, valueXmstrings); n++;
  141.     XtSetArg(args[n], DtNnumValues, numValueStrings); n++;
  142.     XtSetArg(args[n], DtNcolumns, 10); n++;
  143.     spinBox = DtCreateSpinBox(parent, "spinBox1", args, n);
  144.     XtManageChild(spinBox);
  145.  
  146.  
  147.     /*
  148.      * Create a SpinBox containing numeric values to 3 decimal places.
  149.      * Position the arrows on either side of the displayed value.
  150.      */
  151.  
  152.     n = 0; 
  153.     XtSetArg(args[n], DtNspinBoxChildType, DtNUMERIC); n++;
  154.     XtSetArg(args[n], DtNminimumValue, 1000); n++;
  155.     XtSetArg(args[n], DtNmaximumValue, 100000); n++;
  156.     XtSetArg(args[n], DtNincrementValue,1000); n++;
  157.     XtSetArg(args[n], DtNdecimalPoints,3); n++;
  158.     XtSetArg(args[n], DtNposition,1000); n++;
  159.     XtSetArg(args[n], DtNarrowLayout, DtARROWS_SPLIT); n++;
  160.     XtSetArg(args[n], DtNcolumns, 10); n++;
  161.     spinBox = DtCreateSpinBox(parent, "spinBox2", args, n);
  162.     XtManageChild(spinBox);
  163.  
  164.  
  165.     /*
  166.      * Create a SpinBox containing numeric values to 2 decimal places.
  167.      * Position the arrows on the left of the displayed value.
  168.      * Disallow alternate user changes by adding a modify/verify callback.
  169.      */
  170.  
  171.     n = 0; 
  172.     XtSetArg(args[n], DtNspinBoxChildType, DtNUMERIC); n++;
  173.     XtSetArg(args[n], DtNminimumValue, 1500); n++;
  174.     XtSetArg(args[n], DtNmaximumValue, 60500); n++;
  175.     XtSetArg(args[n], DtNincrementValue,1500); n++;
  176.     XtSetArg(args[n], DtNdecimalPoints,2); n++;
  177.     XtSetArg(args[n], DtNposition,7500); n++;
  178.     XtSetArg(args[n], DtNarrowLayout, DtARROWS_FLAT_BEGINNING); n++;
  179.     XtSetArg(args[n], DtNcolumns, 10); n++;
  180.     spinBox = DtCreateSpinBox(parent, "spinBox3", args, n);
  181.     XtManageChild(spinBox);
  182.  
  183.     XtAddCallback(spinBox, DtNmodifyVerifyCallback, ModifyVerifyCb, NULL);
  184.  
  185.  
  186.     /*
  187.      * Create a SpinBox containing string values.
  188.      * Position the arrows on the left of the display value
  189.      */
  190.  
  191.     n = 0; 
  192.     XtSetArg(args[n], DtNvalues, valueXmstrings); n++;
  193.     XtSetArg(args[n], DtNnumValues, numValueStrings); n++;
  194.     XtSetArg(args[n], DtNarrowLayout, DtARROWS_BEGINNING); n++;
  195.     XtSetArg(args[n], DtNcolumns, 10); n++;
  196.     spinBox = DtCreateSpinBox(parent, "spinBox4", args, n);
  197.     XtManageChild(spinBox);
  198.  
  199.  
  200.     /*
  201.      * Create a SpinBox containing numeric values to 3 decimal places.
  202.      * Position the arrows on the right of the displayed value.
  203.      */
  204.  
  205.     n = 0;
  206.     XtSetArg(args[n], DtNspinBoxChildType, DtNUMERIC); n++;
  207.     XtSetArg(args[n], DtNminimumValue, 1000); n++;
  208.     XtSetArg(args[n], DtNmaximumValue, 100000); n++;
  209.     XtSetArg(args[n], DtNincrementValue,1000); n++;
  210.     XtSetArg(args[n], DtNdecimalPoints,3); n++;
  211.     XtSetArg(args[n], DtNposition,1000); n++;
  212.     XtSetArg(args[n], DtNarrowLayout, DtARROWS_FLAT_END); n++;
  213.     XtSetArg(args[n], DtNcolumns, 10); n++;
  214.     spinBox = DtCreateSpinBox(parent, "spinBox5", args, n);
  215.     XtManageChild(spinBox);
  216.  
  217.  
  218.     /*
  219.      * Free value strings, SpinBox has taken a copy.
  220.      */
  221.  
  222.     for (i = 0; i < numValueStrings; i++) {
  223.     XmStringFree(valueXmstrings[i]);
  224.     }
  225.     XtFree((char*)valueXmstrings);
  226.  
  227. }
  228.  
  229.  
  230. /*
  231.  * modify/verify callback.
  232.  *
  233.  * Allow/disallow alternate user changes
  234.  */
  235.  
  236. static void ModifyVerifyCb(Widget w, XtPointer cd, XtPointer cb)
  237. {
  238.     DtSpinBoxCallbackStruct *scb= (DtSpinBoxCallbackStruct*)cb;
  239.     static Boolean allowChange = True;
  240.  
  241.     scb->doit = allowChange;
  242.  
  243.     if (allowChange == False) {
  244.     printf("DtSpinBox: DtNmodifyVerifyCallback. Change disallowed.\n");
  245.     XBell(XtDisplay(w), 0);
  246.     }
  247.  
  248.     allowChange = (allowChange == True) ? False : True;
  249. }
  250.  
  251.  
  252.  
  253. /*
  254.  * Example code for DtComboBox
  255.  */
  256.  
  257.  
  258. static char *comboValueStrings[] = {
  259.     "alpha", "beta", "gamma", "delta",
  260.     "epsilon", "zeta", "eta", "theta",
  261.     "iota", "kappa", "lambda", "mu",
  262.     "nu", "xi", "omicron", "pi",
  263.     "rho", "sigma", "tau", "upsilon",
  264.     "phi", "chi", "psi", "omega"
  265. };
  266.  
  267. static char *colorStrings[] = { "Red", "Yellow", "Green", "Brown", "Blue" };
  268.  
  269. static void CreateComboBoxes(Widget parent)
  270. {
  271.     Widget titleLabel, comboBox, list;
  272.     XmString *valueXmstrings, *colorXmstrings;
  273.     int numValueStrings, numColorStrings;
  274.     XmString labelString, xmString;
  275.     Arg args[20];
  276.     int i, n;
  277.     
  278.     /* Create value compound strings */
  279.  
  280.     numValueStrings = XtNumber(comboValueStrings);
  281.     valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
  282.     for (i = 0; i < numValueStrings; i++) {
  283.     valueXmstrings[i] = XmStringCreateLocalized(comboValueStrings[i]);
  284.     }
  285.  
  286.     /* Create color compound strings */
  287.  
  288.     numColorStrings = XtNumber(colorStrings);
  289.     colorXmstrings = (XmString *)XtMalloc(numColorStrings * sizeof(XmString*));
  290.     for (i = 0; i < numColorStrings; i++) {
  291.     colorXmstrings[i] = XmStringCreateLocalized(colorStrings[i]);
  292.     }
  293.  
  294.     /* Create title label */
  295.  
  296.     labelString = XmStringCreateLocalized("ComboBox Widget");
  297.     n = 0;
  298.     XtSetArg(args[n], XmNlabelString, labelString); n++;
  299.     titleLabel = XmCreateLabel(parent, "title", args, n);
  300.     XtManageChild(titleLabel);
  301.     XmStringFree(labelString);
  302.  
  303.  
  304.     /*
  305.      * Create an editable ComboBox containing the color strings.
  306.      * Get the widget id of the drop down list, add some greek
  307.      * letter names to it, and make more items visible.
  308.      */
  309.  
  310.     n = 0;
  311.     XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
  312.     XtSetArg(args[n], DtNitems, colorXmstrings); n++;
  313.     XtSetArg(args[n], DtNitemCount, numColorStrings); n++;
  314.     XtSetArg(args[n], DtNvisibleItemCount, 5); n++;
  315.     XtSetArg(args[n], DtNcolumns, 10); n++;
  316.     comboBox = DtCreateComboBox(parent, "comboBox1", args, n);
  317.     XtManageChild(comboBox);
  318.  
  319.     list = XtNameToWidget(comboBox, "*List");
  320.     XmListAddItems(list, valueXmstrings, 10, 0);
  321.     XtVaSetValues(list, XmNvisibleItemCount, 10, NULL);
  322.  
  323.  
  324.     /*
  325.      * Create an editable ComboBox with no entries.
  326.      * Get the widget id of the drop down list, add some greek
  327.      * letter names to it and select the third item in the list.
  328.      */
  329.  
  330.     n = 0;
  331.     XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
  332.     XtSetArg(args[n], DtNorientation, DtLEFT); n++;
  333.     XtSetArg(args[n], DtNcolumns, 10); n++;
  334.     comboBox = DtCreateComboBox(parent, "comboBox2", args, n);
  335.     XtManageChild(comboBox);
  336.  
  337.     list = XtNameToWidget(comboBox, "*List");        
  338.     XmListAddItems(list, valueXmstrings, 7, 0);
  339.     XtVaSetValues(list, XmNvisibleItemCount, 7, NULL);
  340.     XtVaSetValues(comboBox, DtNselectedPosition, 3, NULL);
  341.  
  342.  
  343.     /*
  344.      * Create a non-editable ComboBox containing some greek letter names.
  345.      * Position the arrow on the left.
  346.      * Select the 'gamma' item in the list.
  347.      */
  348.  
  349.     n = 0;
  350.     XtSetArg(args[n], DtNorientation, DtLEFT); n++;
  351.     XtSetArg(args[n], DtNitems, valueXmstrings); n++;
  352.     XtSetArg(args[n], DtNitemCount, numValueStrings); n++;
  353.     XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
  354.     comboBox = DtCreateComboBox(parent, "comboBox3", args, n);
  355.     XtManageChild(comboBox);
  356.  
  357.     xmString = XmStringCreateLocalized("gamma");
  358.     XtVaSetValues(comboBox, DtNselectedItem, xmString, NULL);
  359.     XmStringFree(xmString);
  360.  
  361.  
  362.     /*
  363.      * Create a non-editable ComboBox with no entries.
  364.      * Position the arrow on the right.
  365.      * Add the greek letter names to the list and select the fourth item. 
  366.      */
  367.  
  368.     n = 0;
  369.     XtSetArg(args[n], DtNorientation, DtRIGHT); n++;
  370.     XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
  371.     comboBox = DtCreateComboBox(parent, "comboBox4", args, n);
  372.     XtManageChild(comboBox);
  373.  
  374.     for (i = 0; i < numValueStrings; i++) {
  375.     DtComboBoxAddItem(comboBox, valueXmstrings[i], 0, True);
  376.     }
  377.     XtVaSetValues(comboBox, DtNselectedPosition, 4, NULL);
  378.  
  379.  
  380.     /*
  381.      * Free value and color strings, ComboBox has taken a copy.
  382.      */
  383.  
  384.     for (i = 0; i < numValueStrings; i++) {
  385.     XmStringFree(valueXmstrings[i]);
  386.     }
  387.     XtFree((char*)valueXmstrings);
  388.  
  389.     for (i = 0; i < numColorStrings; i++) {
  390.     XmStringFree(colorXmstrings[i]);
  391.     }
  392.     XtFree((char*)colorXmstrings);
  393. }
  394.  
  395.  
  396.  
  397. /*
  398.  * Example code for DtMenuButton
  399.  */
  400.  
  401. /* MenuButton custom glyph */
  402.  
  403. #define menu_glyph_width 16
  404. #define menu_glyph_height 16
  405. static unsigned char menu_glyph_bits[] = {
  406.    0xe0, 0x03, 0x98, 0x0f, 0x84, 0x1f, 0x82, 0x3f, 0x82, 0x3f, 0x81, 0x7f,
  407.    0x81, 0x7f, 0xff, 0x7f, 0xff, 0x40, 0xff, 0x40, 0xfe, 0x20, 0xfe, 0x20,
  408.    0xfc, 0x10, 0xf8, 0x0c, 0xe0, 0x03, 0x00, 0x00};
  409.  
  410. static void CreateMenuButtons(Widget parent)
  411. {
  412.     Widget menuButton, submenu, titleLabel, button;
  413.     Pixmap cascadePixmap;
  414.     Pixel fg, bg;
  415.     Cardinal depth;
  416.     XmString labelString;
  417.     Arg args[20];
  418.     int i, n;
  419.  
  420.     /* Create title label */
  421.  
  422.     labelString = XmStringCreateLocalized("MenuButton Widget");
  423.     n = 0;
  424.     XtSetArg(args[n], XmNlabelString, labelString); n++;
  425.     titleLabel = XmCreateLabel(parent, "title", args, n);
  426.     XtManageChild(titleLabel);
  427.     XmStringFree(labelString);
  428.  
  429.  
  430.     /*
  431.      * Create a MenuButton.
  432.      * Add push buttons to the built-in popup menu.
  433.      */
  434.  
  435.     labelString = XmStringCreateLocalized("Action");
  436.     n = 0;
  437.     XtSetArg(args[n], XmNlabelString, labelString); n++;
  438.     menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
  439.     XtManageChild(menuButton);
  440.     XmStringFree(labelString);
  441.  
  442.     XtVaGetValues(menuButton, DtNsubMenuId, &submenu, NULL);
  443.     button = XmCreatePushButton(submenu, "Push", NULL, 0);
  444.     XtManageChild(button);
  445.     button = XmCreatePushButton(submenu, "Pull", NULL, 0);
  446.     XtManageChild(button);
  447.     button = XmCreatePushButton(submenu, "Turn", NULL, 0);
  448.     XtManageChild(button);
  449.  
  450.  
  451.     /*
  452.      * Create a MenuButton.
  453.      * Replace the built-in popup menu with a tear-off menu.
  454.      * Add a custom pixmap in the colors of the MenuButton.
  455.      */
  456.  
  457.     labelString = XmStringCreateLocalized("Movement");
  458.     n = 0;
  459.     XtSetArg(args[n], XmNlabelString, labelString); n++;
  460.     menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
  461.     XtManageChild(menuButton);
  462.     XmStringFree(labelString);
  463.  
  464.     /* Create a tear-off menu */
  465.  
  466.     n = 0;
  467.     XtSetArg(args[0], XmNtearOffModel, XmTEAR_OFF_ENABLED); n++;
  468.     submenu = XmCreatePopupMenu(menuButton, "submenu", args, n);
  469.     button = XmCreatePushButton(submenu, "Run", NULL, 0);
  470.     XtManageChild(button);
  471.     button = XmCreatePushButton(submenu, "Jump", NULL, 0);
  472.     XtManageChild(button);
  473.     button = XmCreatePushButton(submenu, "Stop", NULL, 0);
  474.     XtManageChild(button);
  475.  
  476.     XtVaSetValues(menuButton, DtNsubMenuId, submenu, NULL);
  477.  
  478.     /* Create a pixmap using the menu button's colors and depth */
  479.  
  480.     XtVaGetValues(menuButton,
  481.             XmNforeground, &fg,
  482.             XmNbackground, &bg,
  483.             XmNdepth, &depth,
  484.             NULL);
  485.  
  486.     cascadePixmap = XCreatePixmapFromBitmapData(XtDisplay(menuButton),
  487.                 DefaultRootWindow(XtDisplay(menuButton)),
  488.                 (char*)menu_glyph_bits,
  489.                 menu_glyph_width, menu_glyph_height,
  490.                 fg, bg, depth);
  491.     XtVaSetValues(menuButton, DtNcascadePixmap, cascadePixmap, NULL);
  492. }
  493.  
  494.